//
//  TransactionViewController.m
//  Futures
//
//  Created by Cheng on 2017/6/20.
//  Copyright © 2017年 Cheng. All rights reserved.
//

#import "TransactionViewController.h"
#import "TransactionCell.h"
#import "HomeViewModel.h"
#import "QuoteModel.h"
#import "TransacionViewModel.h"
#import "JX_GCDTimerManager.h"
#define kTransactionCellID @"kTransactionCellID"
#define kGetQuoteList_IN_TRANS @"kGetQuoteList_IN_TRANS"


@interface TransactionViewController () <UITableViewDelegate,UITableViewDataSource>
{
    NSArray *_quoteList;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation TransactionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"行情交易";
    
    self.tableView.delegate = self;
    
    self.tableView.dataSource = self;
    
    [self refreshData];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
}

- (void)refreshData{
    [TransacionViewModel requestSymbolsWithBlock:^(id obj) {
        _quoteList = obj;
        [self.tableView reloadData];
    }];
    
//    [HomeViewModel requestQuoteList:^(NSArray *quoteList) {
//        _quoteList = quoteList;
//        [self.tableView reloadData];
//    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _quoteList.count;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TransactionCell *cell = [tableView dequeueReusableCellWithIdentifier:kTransactionCellID forIndexPath:indexPath];
    
    QuoteModel *model = _quoteList[indexPath.row];
    
    [cell setCellWithModel:model];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    QuoteModel *model = _quoteList[indexPath.row];
    [Global shareInstance].quoteModel = model;
    UIViewController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"TabBarController"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    __weak typeof(self) weakSelf = self;
    [[JX_GCDTimerManager sharedInstance] scheduledDispatchTimerWithName:kGetQuoteList_IN_TRANS timeInterval:5.0f queue:nil repeats:YES actionOption:AbandonPreviousAction action:^{
        // 请求合约报价
        [TransacionViewModel requestSymbolsWithBlock:^(id obj) {
            _quoteList = obj;
            [weakSelf.tableView reloadData];
        }];
    }];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    if ([[JX_GCDTimerManager sharedInstance]existTimer:kGetQuoteList_IN_TRANS]) {
        [[JX_GCDTimerManager sharedInstance] cancelTimerWithName:kGetQuoteList_IN_TRANS];
    }
}
    
@end
